package edu.ccut.saturn.studio.core.util.eclipse;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.events.ExpansionAdapter;
import org.eclipse.ui.forms.events.ExpansionEvent;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

public abstract class SaturnSectionPart extends SectionPart {

	public SaturnSectionPart(Composite parent, IManagedForm form) {
		super(parent, form.getToolkit(), Section.DESCRIPTION|ExpandableComposite.TWISTIE|ExpandableComposite.EXPANDED|ExpandableComposite.TITLE_BAR);
		getSection().addExpansionListener(new ExpansionAdapter() {
			public void expansionStateChanged(ExpansionEvent e) {
				getManagedForm().getForm().reflow(true);
			}
		});
		//设置部分标题信息
		getSection().setText(getSectionTitle());
		//设置标题下面的描述
		getSection().setDescription(getSectionDescription());
	}

	abstract protected String getSectionDescription();
	abstract protected String getSectionTitle();
	
	protected void adaptRecursively(FormToolkit tk, Composite composite) {
		tk.adapt(composite);
		Control[] children = composite.getChildren();
		for (int i = 0; i < children.length; i++) {
			Control control = children[i];
			if (control instanceof Composite) {
				adaptRecursively(tk, (Composite) control);
			} else {
				tk.adapt(control, true, false);
			}
		}
	}
	
	public void dispose() {
		super.dispose();
	}
}
